<?php
namespace app\forumadmin_guzp\controller;
use think\facade\View;
use think\facade\Db;
class Deal extends \app\BaseController
{
	// 帖子管理 C组
    public function mesDeal($s_id=1)
    {
        // 检测登录状态及ID合法性 C组
        $this->checka();
        $this->checkId($s_id,'sec');
        // 加载版块 C组
        $sec = DB::table('guzp_section')
            ->where('article_is_delete',1)
            ->order('s_id')
            ->select();
        $cur = DB::table('guzp_section')
            ->where('article_is_delete',1)
            ->where('s_id',$s_id)
            ->find();
        // 判断是否执行搜索 C组
        if (!input('keyword')) {
            // 加载版块内的所有帖子 C组
            $mes = Db::table('guzp_mes')
                ->where('article_is_delete',1)
                ->order('m_id');
            if($s_id == 1){
                $mes = $mes;
            }
            else{
                $mes = $mes->where('s_id',$s_id);
            }
                
        } else {
            // 模糊查询 C组
            $mes = Db::table('guzp_mes')
                ->order('m_id')
                ->where('s_id',$s_id)
                ->where('article_is_delete',1)
                ->where('m_title','like','%'.input('keyword').'%')
                ->whereOr('u_nick','like','%'.input('keyword').'%');
        } 
        $mes = $mes
            ->paginate([
                'list_rows'=>3,
                'query'=> request()->param(),
            ]);
        $page = $mes->render();      
        return view('',['sec'=>$sec,'cur'=>$cur,'mes'=>$mes,'page'=>$page]);
    }
	// 帖子详细页管理 C组
    public function aDetail($m_id=-1)
    {
        $this->checka();
        $this->checkId($m_id,'mes');
        $mes = Db::table('guzp_mes')
            ->where('article_is_delete',1)
            ->where('m_id',$m_id)
            ->find();
        $res = Db::table('guzp_res')
            ->order('r_id')
            ->where('article_is_delete',1)
            ->where('m_id',$m_id)
            ->select();
        return view('',['mes'=>$mes,'res'=>$res]);
    }
    // 帖子回收站管理
    public function mesRecycle(){
        $mes = DB::table('guzp_mes')
            ->where('m_id',input('m_id'))
            ->find();
        return view('',['mes' => $mes]);
    }
    // 将贴放入回收站
    public function domesRecycle(){
        // 将因原帖被放入回收站的回复信息放入guzp_resrecycle表
        // 将因原帖被放入回收的回复的article_is_delete值改为+2
        $res = DB::table('guzp_res')
            ->where('m_id',input('mid'))
            ->field('article_is_delete,r_id,m_id')
            ->select()
            ->toArray();
        $length = count($res);
        // return;
        for($i = 0;$i<$length;$i++){
            if($res[$i]['article_is_delete'] == 2){
                $resUpdate = DB::table('guzp_res')
                    ->where('r_id',$res[$i]['r_id'])
                    ->update(['article_is_delete' => 4]);
            }
            else{
                $resRecycle = [
                'r_id' => $res[$i]['r_id'],
                'm_id' => input('m_id'),
                'r_reason' => "原帖被放入回收站",
                'rrecycle_time' => time()
                ];
                $resrecycle = DB::table('guzp_resrecycle')
                    ->insert($resRecycle);
                $resUpdate = DB::table('guzp_res')
                    ->where('r_id',$res[$i]['r_id'])
                    ->update(['article_is_delete' => 3]);
            }   
        }
        // return;
        // 将原帖的article_is_delete值改为2
        $mes = DB::table('guzp_mes')
            ->where('m_id',input('mid'))
            ->update(['article_is_delete'=>2]);
        // 将原帖放入回收站的信息放入guzp_mrecycle
        $mesRecycle = [
            'm_id' => input('mid'),
            'm_reason' => input('m_reason'),
            'mrecycle_time' => time()
        ];
        $domesRecycle = DB::table('guzp_mrecycle')
            ->insert($mesRecycle);
        if($domesRecycle){
            $this->success('回收帖子成功','deal/mesDeal');
        }
        else{
            $this->error('回收失败');
            exit();
        }
    }
    // 删帖 C组
    public function mesDel($m_id=-1)
    {
        $this->checka();
        $this->checkId($m_id,'mes');
        $resrecycle = DB::table('guzp_resrecycle')
            ->where('m_id',$m_id)
            ->delete();
        $res = Db::table('guzp_res')
            ->where('m_id',$m_id)
            ->delete();
        $mes = Db::table('guzp_mes')
            ->where('m_id',$m_id)
            ->delete();
        if($mes){
            $this->success('删帖成功！');
        }
        else{
            $this->error('删帖失败');
        }
        
    }
    // 删回复 C组
    public function resDel($r_id=-1)
    {
        $this->checka();
        $this->checkId($r_id,'res');
        $res = Db::table('guzp_res')
            ->where('r_id',$r_id)
            ->delete();
        $this->success('删回复成功！');
    }
    // 回收回复
    public function resRecycle()
    {
        $res = DB::view('guzp_res','r_id,r_content,m_id')
            ->where('r_id',input('r_id'))
            ->view('guzp_mes','m_title,m_id','guzp_mes.m_id = guzp_res.m_id')
            ->find();
        return view('',['res' => $res]);
    }
    // 处理回复回收站提交信息
    public function doresRecycle(){
        $resRecycle = [
            'r_id' => input('r_id'),
            'm_id' => input('m_id'),
            'r_reason' => input('r_reason'),
            'rrecycle_time' => time()
        ];
        $resInsert = DB::table('guzp_resrecycle')
            ->insert($resRecycle);
        $res = DB::table('guzp_res')
            ->where('r_id',input('r_id'))
            ->update(['article_is_delete' => 2]);
        if($resInsert && $res){
            $this -> success('放入成功',url('deal/aDetail',['m_id'=>input('m_id')]));
        }
        else{
            $this->error('放入失败',url('deal/aDetail',['m_id'=>input('m_id')]));
        }
    }
    public function secdeal(){
        // 检测登录状态 C组
        $this->checka();
        if(!input('find')){
            $sec = DB::table('guzp_section')
            ->where('article_is_delete',1)
            ->field('s_name,s_id,s_remark')
            ->paginate([
                'list_rows' => 3,
                'query' => request()->param(),
            ]);
        }else{
            // 模糊查询
            $sec = DB::table('guzp_section')
            ->where('article_is_delete',1)
            ->where('s_name','like','%'.input('find').'%')
            ->whereor('s_remark','like','%'.input('find').'%')
            ->field('s_name,s_id,s_remark')
            ->paginate([
                'list_rows' => 3,
                'query' => request()->param(),
            ]);
        }
        $page = $sec->render();
        return view('',['sec' => $sec,'page' => $page]);
    }
    // 创建板块
    public function seccreat(){
        // 检测登录状态 C组
        $this->checka();
        $sec =[
            's_name' => input('s_name'),
            's_remark' => input('s_remark')
        ];
        $secPost = DB::table('guzp_section')
            ->insert($sec);
        if ($secPost){
            $this -> success("创建成功",url('deal/secdeal'),'',3);
        }
        else{
            $this->error("创建失败");
        }
    }
    // 修改板块信息 C组
    public function secupdate(){
        // 检测登录状态 C组
        $this->checka();
        $paUpdate = DB::table('guzp_section')
                ->where('s_id',input('sid'))
                ->update(['s_name' => input('sname'),'s_remark' => input('remark')]);
        if($paUpdate){
            $this -> success("修改板块信息成功",url('deal/secdeal'),'',3);
        }
        else{
            $this->error('修改失败');
        }
    }
    // 将板块放到回收站
    public function resrec(){
        // 检测登录状态 C组
        $this->checka();
        // 1、将guzp_section.article_is_delete的值改为2，
        // 2、并将s_id及放入回收站原因插入guzp_guzp_srecycle。
        // 3、其板块下方所属的帖子guzp_mes.article_is_delete的值+2,
        // 4、并将该m_id及“因所属板块被放入回收站”插入guzp_mrecycle。
        // 5、其所属的所有回复均将guzp_res.article_is_delete值改为+2。
        // 获得板块下对应的所有m_id
        $mes = DB::table('guzp_mes')
            ->where('s_id',input('remark_id'))
            ->field('s_id,m_id,article_is_delete')
            ->select()
            ->toArray();
        // 获得板块下所有对应的回复r_id
        $res = DB::view('guzp_mes','s_id,m_id')
            ->where('s_id',input('remark_id'))
            ->view('guzp_res','r_id,m_id,article_is_delete,r_content','guzp_res.m_id = guzp_mes.m_id')
            ->select()
            ->toArray();
        // 将所有帖子article_is_delete值+2
        // 如果帖子处于正常状态即article_is_delete值为1，则在guzp_mrecycle表中插入列
        for($i = 0;$i<count($mes);$i++){
            $number = $mes[$i]['article_is_delete'];
            $mesUpate = DB::table('guzp_mes')
                ->where('m_id',$mes[$i]['m_id'])
                ->update(['article_is_delete' => $number+2]);
            if($number == 1){
                $mrecycle = [
                    'm_id' => $mes[$i]['m_id'],
                    'm_reason' => "原板块被放入回收站",
                    'mrecycle_time' => time()
                ];
                $guzp_mrecycle = DB::table('guzp_mrecycle')
                    ->insert($mrecycle);
            }
        }
        // 将所有回复article_is_delete的值+2
        // 如果回复处于正常状态即article_is_delete的值为1，则在guzp_resrecycle表中插入列
        for($i = 0;$i<count($res);$i++){    
            $number = $res[$i]['article_is_delete'];
            $resUpdate = DB::table('guzp_res')
                ->where('r_id',$res[$i]['r_id'])
                ->update(['article_is_delete' => $number +2]);
            if($number == 1){
                $resrecycle = [
                    'r_id' => $res[$i]['r_id'],
                    'm_id' => $res[$i]['m_id'],
                    'r_reason' => "原板块被放入回收站",
                    'rrecycle_time' => time()
                ];
                $recycle = DB::table('guzp_resrecycle')
                    ->insert($resrecycle);
            }
        }
        // 将板块article_is_delete的值改为2
        $secycle = DB::table('guzp_section')
            ->where('s_id',input('remark_id'))
            ->update(['article_is_delete' => 2 ]);
        // 在guzp_srecycle表中插入列
        $srecycle = [
            's_id' => input('remark_id'),
            's_reason' => input('s_reason'),
            'recycle_time' => time()
        ];
        $guzp_srecycle = DB::table('guzp_srecycle')
            ->insert($srecycle);
        if($secycle && $guzp_srecycle){
            $this -> success("已成功放入回收站",url('deal/secdeal'),'',3);
        }
        else{
            $this->error('放入失败');
        }
    }
}